package com.zsjt.app.compoent.security.session;

import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * session无效处理
 */
@Component("invalidSessionStrategyImpl")
public class InvalidSessionStrategyImpl implements InvalidSessionStrategy {

    @Override
    public void onInvalidSessionDetected(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
//        Cookie[] cookies = httpServletRequest.getCookies();
//        if(cookies!=null){
//            for(Cookie cookie:cookies){
//                System.out.println(cookie.getName()+"------"+cookie.getValue());
//                cookie.setMaxAge(0);
//            }
//        }
        // 自定义session无效处理
        System.out.println("session超时，请重新登录");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().append("session无效，请重新登录");
    }
}
